package com.zhenta.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhenta.service.AppointmentService;
import com.zhentao.mapper.AppointmentMapper;
import com.zhentao.pojo.Appointment;
import com.zhentao.pojo.AppointmentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 预约挂号服务实现类
 */
@Service
public class AppointmentServiceImpl extends ServiceImpl<AppointmentMapper, Appointment> implements AppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;

    @Override
    public List<AppointmentVO> getAppointmentList() {
        List<AppointmentVO> appointmentList = appointmentMapper.selectAppointmentList();
        // 设置状态文本和样式类，以及前端期望的字段
        for (AppointmentVO appointment : appointmentList) {
            setStatusInfo(appointment);
            setFrontendFields(appointment);
        }
        return appointmentList;
    }

    @Override
    public AppointmentVO getAppointmentById(Integer id) {
        AppointmentVO appointment = appointmentMapper.selectAppointmentById(id);
        if (appointment != null) {
            setStatusInfo(appointment);
            setFrontendFields(appointment);
        }
        return appointment;
    }

    @Override
    public List<AppointmentVO> getAppointmentsByPatientId(Integer patientId) {
        List<AppointmentVO> appointmentList = appointmentMapper.selectAppointmentsByPatientId(patientId);
        // 设置状态文本和样式类，以及前端期望的字段
        for (AppointmentVO appointment : appointmentList) {
            setStatusInfo(appointment);
            setFrontendFields(appointment);
        }
        return appointmentList;
    }

    @Override
    public List<AppointmentVO> getAppointmentsByStatus(Integer status) {
        List<AppointmentVO> appointmentList = appointmentMapper.selectAppointmentsByStatus(status);
        // 设置状态文本和样式类，以及前端期望的字段
        for (AppointmentVO appointment : appointmentList) {
            setStatusInfo(appointment);
            setFrontendFields(appointment);
        }
        return appointmentList;
    }

    @Override
    public boolean createAppointment(Appointment appointment) {
        appointment.setStatus(0); // 预约成功
        appointment.setYyTime(new Date()); // 设置预约时间
        return this.save(appointment);
    }

    @Override
    public boolean cancelAppointment(Integer id) {
        LambdaUpdateWrapper<Appointment> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Appointment::getGid, id)
                     .set(Appointment::getStatus, 2) // 已取消
                     .set(Appointment::getYyTime, new Date()); // 更新预约时间
        return this.update(updateWrapper);
    }

    @Override
    public boolean completeAppointment(Integer id) {
        LambdaUpdateWrapper<Appointment> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Appointment::getGid, id)
                     .set(Appointment::getStatus, 1) // 已完成
                     .set(Appointment::getByTime, new Date()); // 设置就诊时间
        return this.update(updateWrapper);
    }

    @Override
    public boolean updateAppointment(Appointment appointment) {
        return this.updateById(appointment);
    }

    @Override
    public boolean deleteAppointment(Integer id) {
        return this.removeById(id);
    }

    /**
     * 设置状态信息
     */
    private void setStatusInfo(AppointmentVO appointment) {
        switch (appointment.getStatus()) {
            case 0:
                appointment.setStatusText("预约成功");
                appointment.setStatusClass("status-success");
                break;
            case 1:
                appointment.setStatusText("已完成");
                appointment.setStatusClass("status-completed");
                break;
            case 2:
                appointment.setStatusText("已取消");
                appointment.setStatusClass("status-cancelled");
                break;
            default:
                appointment.setStatusText("未知状态");
                appointment.setStatusClass("status-unknown");
        }
    }
    
    /**
     * 设置前端期望的字段
     */
    private void setFrontendFields(AppointmentVO appointment) {
        // 设置科室名称
        appointment.setDepartmentName(appointment.getType());
        
        // 设置医生姓名（根据医生ID设置，这里暂时使用默认值）
        String doctorName = getDoctorNameById(appointment.getDoctorId());
        appointment.setDoctorName(doctorName);
        
        // 设置患者姓名
        appointment.setPatientName(appointment.getUsername());
        
        // 设置预约日期
        if (appointment.getByData() != null) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            appointment.setAppointmentDate(dateFormat.format(appointment.getByData()));
        } else {
            appointment.setAppointmentDate("");
        }
        
        // 设置预约时间
        if (appointment.getByTime() != null) {
            SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
            appointment.setAppointmentTime(timeFormat.format(appointment.getByTime()));
        } else {
            appointment.setAppointmentTime("");
        }
        
        // 设置预约ID
        appointment.setId(appointment.getGid());
    }
    
    /**
     * 根据医生ID获取医生姓名
     */
    private String getDoctorNameById(Integer doctorId) {
        if (doctorId == null) {
            return "待分配";
        }
        
        // 这里可以根据医生ID从医生表中查询，暂时使用默认值
        switch (doctorId) {
            case 1:
                return "张医生";
            case 2:
                return "李医生";
            case 3:
                return "王医生";
            default:
                return "医生" + doctorId;
        }
    }
} 